package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-12-01
 */
//医院设置接口
@Api(tags = "医院设置接口")
@RestController  //返回json数据
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin //解决跨域
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    // 医院设置锁定和解锁
    //id 医院设置id  status 状态 0 1
    @PutMapping("lockHospSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //根据id查询
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态值
        hospitalSet.setStatus(status);
        //修改
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //批量删除接口
    //  [1,2,3]
    @ApiOperation("批量删除")
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Long> idList) {
        //传递集合  [1,2,3]
        hospitalSetService.removeByIds(idList);
        return R.ok();
    }

    //根据id修改
    //  {.....}
    @ApiOperation("修改医院设置")
    @PostMapping("updateHospSet")
    public R updateHospSet(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("getHospSet/{id}")
    public R getHospSet(@PathVariable String id) {
        //TODO 模拟异常
//        try {
//            int i = 8/0;
//        }catch(Exception e) {
//            //抛出自定义异常
//            throw new YyghException(20001,"出现了自定义异常处理");
//        }
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("hospitalSet",hospitalSet);
    }

    //添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("saveHospSet")
    public R saveHospSet(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    // @RequestBody
    // 提交json数据，使用注解获取json数据进行封装
    // post提交一起使用  get提交使用得不到数据， 其他提交方式delete put数据不完整
    //条件查询带分页接口
    //医院名称+医院编号查询
    @ApiOperation("条件查询带分页")
    @PostMapping("hospSetPageQuery/{current}/{limit}")
    public R pageQuery(@PathVariable Long current, @PathVariable Long limit,
                       @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象
        Page<HospitalSet> pageModel = new Page<>(current,limit);
        //如果条件对象为空，查询全部
        if(hospitalSetQueryVo == null) {
            //调用方法实现查询全部
            hospitalSetService.page(pageModel);
        } else {//如果条件对象不为空
            //构建条件
            String hoscode = hospitalSetQueryVo.getHoscode();
            String hosname = hospitalSetQueryVo.getHosname();
            //非空判断
            QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(hoscode)) {
                //封装条件
                wrapper.eq("hoscode",hoscode);
            }
            if(!StringUtils.isEmpty(hosname)) {
                //封装条件
                wrapper.like("hosname",hosname);
            }
            //调用方法
            hospitalSetService.page(pageModel,wrapper);
        }
        // 返回结果
        List<HospitalSet> list = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total",total).data("rows",list);
    }

    //分页查询接口
    // current当前页   limit每页显示记录数
    @ApiOperation("分页查询")
    @GetMapping("hospSetPage/{current}/{limit}")
    public R pageList(@PathVariable Long current,
                      @PathVariable Long limit) {
        // 创建page对象，传递分页参数
        Page<HospitalSet> pageModel = new Page<>(current,limit);

        // 调用方法实现分页查询
        //pageModel分页查询之后所有数据
        hospitalSetService.page(pageModel, null);

        // 返回结果
        List<HospitalSet> list = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total",total).data("rows",list);
    }

    // http://localhost:8201/swagger-ui.html
    // http://localhost:8201/admin/hosp/hospitalSet/remove/1
    //get提交浏览器可以直接访问测试，但是delete提交浏览器没有直接测试
    //2 逻辑删除医院设置
//    @ApiOperation("删除医院设置")
//    @DeleteMapping("remove/{id}")
//    public boolean removeHospSet(@PathVariable String id) {
//        //调用service的删除方法
//        boolean isSuccess = hospitalSetService.removeById(id);
//        return isSuccess;
//    }

    // http://localhost:8201/admin/hosp/hospitalSet/findAll
    //1 查询所有医院设置数据
//    @ApiOperation("医院设置列表")
//    @GetMapping("findAll")
//    public List<HospitalSet> getAllHospSet() {
//        //调用service方法
//        List<HospitalSet> list = hospitalSetService.list();
//        return list;
//    }

    @ApiOperation("删除医院设置")
    @DeleteMapping("remove/{id}")
    public R removeHospSet(@PathVariable String id) {
        //调用service的删除方法
        boolean isSuccess = hospitalSetService.removeById(id);
        if(isSuccess) {//成功
//            R r = new R();
//            r.setSuccess(true);
//            r.setCode(20000);
//            r.setMessage("成功");
//            //返回对象
            return R.ok().message("删除成功");
        } else { //失败
//            R r = new R();
//            r.setSuccess(false);
//            r.setCode(20001);
//            r.setMessage("失败");
            //返回对象
            return R.error().message("删除失败");
        }
    }

    //使用统一返回结果
    @ApiOperation("医院设置列表")
    @GetMapping("findAll")
    public R getAllHospSet() {
        //调用service方法
        List<HospitalSet> list = hospitalSetService.list();

        //封装返回结果对象
//        R r = new R();
//        r.setSuccess(true);
//        r.setCode(20000);
//        r.setMessage("成功");
//        Map<String, Object> data = new HashMap<String, Object>();
//        data.put("items",list);
//        r.setData(data);
        //返回对象
        return R.ok().data("items",list);
    }

}

